/*
 * Copyright (c) 2009-2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_DIRECTX_RENDER_TARGET_H_INCLUDED_
#define ALKES_DIRECTX_RENDER_TARGET_H_INCLUDED_

#include "alkes/gfx/IRenderTarget3D.h"
#include "alkes/directx9/DirectXGraphics.h"

namespace alkes {

/// \addtogroup DirectX9
//@{

class AL_DX9_EXPORT DirectXRenderTarget
    : public IRenderTarget3D
{
    AL_DECLARE_CLASS(DirectXRenderTarget, IRenderTarget2D);

public:
    static DirectXRenderTarget* createBackSurface(DirectXGraphics& directx);

    // 3D interface
    virtual void beginDraw3D();
    virtual void endDraw3D();

    virtual void setViewport(const Viewport& viewport);
    virtual const Viewport getViewport() const;
    virtual void setPerspective(float angle, float aspect, float znear, float zfar);
    virtual void setLookAt(const Vector3D& eye, const Vector3D& at, const Vector3D& up);
    virtual void translate(float x, float y, float z);
    virtual void scale(float xscale, float yscale, float zscale);
    virtual void rotate(float angle, float x, float y, float z);

    virtual void clearBuffer(uint32_t buffer_flag, const Color& clear_color, const Rect* rect = 0, float depth = -1.0f);
    virtual bool drawPrimitive(IVertexBuffer* vbuffer, PrimitiveType type, uint32_t start, uint32_t count);
    virtual bool setTexture(Image* image, int stage = 0);
    virtual void setTextureFilter(TexFilterValue min_filter, TexFilterValue mag_filter, int stage);
    virtual void getTextureFilter(TexFilterValue* min_filter, TexFilterValue* mag_filter, int stage);
    virtual void setTextureAddressMode(TexAddressMode u_mode, TexAddressMode v_mode, int stage);
    virtual void getTextureAddressMode(TexAddressMode* u_mode, TexAddressMode* v_mode, int stage);

    // special
    void onResetDevice();

    // 2D interface
    virtual void beginDraw2D();
    virtual void endDraw2D();

    virtual const Rect getTargetRect() const;

    virtual void setScissorRect(const Rect& rect);
    virtual const Rect getScissorRect();

    virtual void setState(int state, bool enable);
    virtual bool getState(int state);

    virtual void setAlphaBlendMode(AlphaBlendMode src_blend, AlphaBlendMode dst_blend);
    virtual void getAlphaBlendMode(AlphaBlendMode* src_blend, AlphaBlendMode* dst_blend);

    virtual void pushMatrix();
    virtual void popMatrix();
    virtual void translate(float x, float y);
    virtual void translateForImage(float x, float y);
    virtual void scale(float xscale, float yscale, const Vector2D& origin = Vector2D::ZERO);
    virtual void rotate(float angle, const Vector2D& origin = Vector2D::ZERO);
    virtual void skewX(float angle);
    virtual void skewY(float angle);

    // Device
    void releaseSurface();
    void setSurface(IDirect3DSurface* new_surface);

protected:
    DirectXRenderTarget(DirectXGraphics& directx, const Rect& rect, TargetBehavior target_behavior);
    DirectXRenderTarget(DirectXGraphics& directx, IDirect3DSurface* target, TargetBehavior target_behavior);
    virtual ~DirectXRenderTarget();
    virtual void destroy();

    DirectXGraphics* getDirectXGraphics() const;

    virtual void onSnapshot(ImageWriter& writer, const Rect& rect);

private:
    IDirect3DSurface* render_target_;
    Rect rect_;
};

//@}

}

#endif
